<?php
if (!(defined('IN_IA'))) 
{
    exit('Access Denied');
}

class Verifygoods_MxShopModel {

    /**
     *
     * @param type $orderid
     */
    public function createverifygoods($orderid) {

        global $_W;

        $verifygoods = pdo_fetchall('select  *   from  ' . tablename('mx_shop_verifygoods') . ' where  orderid=:orderid ', array(':orderid' => $orderid));

        if(!empty($verifygoods))
        {
            return false;
        }

        if(p('newstore'))
        {
            $sql2 = ",o.storeid,o.isnewstore";
        }

        $ordergoods = pdo_fetchall('select o.openid,o.uniacid,o.id as orderid , og.id as ordergoodsid,g.verifygoodsdays,g.verifygoodsnum,g.verifygoodslimittype,g.verifygoodslimitdate,og.total '.$sql2.' from ' . tablename('mx_shop_order_goods') . '   og inner join ' . tablename('mx_shop_goods') . ' g on og.goodsid = g.id
          inner join ' . tablename('mx_shop_order') . ' o on og.orderid = o.id
          where   og.orderid =:orderid and g.type = 5', array(':orderid' => $orderid));

        $time = time();

        foreach($ordergoods  as  $ordergood)
        {
            $total = intval($ordergood['total']);
            for($i=0;$i<$total;$i++)
            {
                $data=array(
                    "uniacid"=>$ordergood['uniacid'],
                    "openid"=>$ordergood['openid'],
                    "orderid"=>$ordergood['orderid'],
                    "ordergoodsid"=>$ordergood['ordergoodsid'],
                    "starttime"=>$time,
                    "limittype"=>intval($ordergood['verifygoodslimittype']),
                    "limitdate"=>intval($ordergood['verifygoodslimitdate']),
                    "limitdays"=>intval($ordergood['verifygoodsdays']),
                    "limitnum"=>intval($ordergood['verifygoodsnum']),
                    "used"=>0,
                    "invalid"=>0
                );

                if(p('newstore'))
                {
                    if(!empty($ordergoods['storeid'])&&!empty($ordergoods['isnewstore']))
                    {
                        $data["storeid"]=intval($ordergoods['storeid']);
                    }
                }

                pdo_insert("mx_shop_verifygoods",$data);
            }
        }

        return true;
    }



    /**
     *
     * @param type $openid
     */
    public function getCanUseVerifygoods($openid)
    {
        global $_W;
        $sql = 'select vg.*,g.title,g.subtitle,g.thumb,c.card_id  from ' . tablename('mx_shop_verifygoods') . '   vg
                 inner join ' . tablename('mx_shop_order_goods') . ' og on vg.ordergoodsid = og.id
                 left  join ' . tablename('mx_shop_order') . ' o on vg.orderid = o.id
                 left  join ' . tablename('mx_shop_order_refund') . ' orf on o.refundid = orf.id
                 inner join ' . tablename('mx_shop_goods') . ' g on og.goodsid = g.id
                 left  join ' . tablename('mx_shop_goods_cards') . ' c on c.id = g.cardid
                 where   vg.uniacid=:uniacid and vg.openid=:openid and vg.invalid =0 and (o.refundid=0 or orf.status<0) and o.status>0
                 and    ((vg.limittype=0   and vg.limitdays * 86400 + vg.starttime >=unix_timestamp() )or ( vg.limittype=1   and vg.limitdate >=unix_timestamp() ))  and  vg.used =0 order by vg.starttime desc';

        $verifygoods = set_medias(pdo_fetchall($sql, array(':uniacid' => $_W['uniacid'], ':openid' => $openid)), 'thumb');

        if (empty($verifygoods)) {
            $verifygoods = array();
        }

        foreach ($verifygoods as $i => &$row) {

            $row['numlimit'] = 0;

            if (!empty($row['limitnum'])) {
                $row['numlimit'] = 1;
            }

            if (is_weixin()) {
                if (!empty($row['card_id']) && empty($row['activecard'])) {
                    $row['cangetcard'] = 1;
                }
            }
        }

        unset($row);

        return $verifygoods;

    }



    /**
     *
     * @param type $openid
     */
    public function checkhaveverifygoods($openid)
    {
        global $_W;
        $sql = 'select  COUNT(1)  from ' . tablename('mx_shop_verifygoods') . '   vg
                 inner join ' . tablename('mx_shop_order_goods') . ' og on vg.ordergoodsid = og.id
                 left  join ' . tablename('mx_shop_order') . ' o on vg.orderid = o.id
                 left  join ' . tablename('mx_shop_order_refund') . ' orf on o.refundid = orf.id
                 inner join ' . tablename('mx_shop_goods') . ' g on og.goodsid = g.id
                 left  join ' . tablename('mx_shop_goods_cards') . ' c on c.id = g.cardid
                 where   vg.uniacid=:uniacid and vg.openid=:openid and vg.invalid =0   order by vg.starttime desc';

        $verifygoods = pdo_fetchcolumn($sql, array(':uniacid' => $_W['uniacid'], ':openid' => $openid));

        if (!empty($verifygoods)) {
            return 1;
        }

        return 0;

    }


}
